package com.bona.dao.system;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bona.dao.common.BaseDao;
import com.bona.model.system.SysRoleFunctionModel;
import com.bona.model.system.SysRoleModel;
import com.bona.model.system.SysRoleOrgModel;

public interface SysRoleDao extends BaseDao {
	
	/**
	 * 查询用户拥有的角色
	 * @param userId
	 * @return
	 */
	@Select("select id, rolecode, rolename, update_name \"updateName\", " +
	 "update_date \"updateDate\", update_by \"updateBy\", create_name \"createName\", " + 
	 "create_date \"createDate\", create_by \"createBy\" " +
	 "from sys_role where id IN (SELECT roleid FROM sys_role_user WHERE userid=#{userId})")
	@RequestMapping("SysRoleModel")
	public List<SysRoleModel> queryRoleByUserId(@Param("userId")String userId);
	
	/**
	 * 查询机构关联的角色
	 * @param userId
	 * @return
	 */
	@Select("select id, rolecode, rolename, update_name \"updateName\", " +
	 "update_date \"updateDate\", update_by \"updateBy\", create_name \"createName\", " + 
	 "create_date \"createDate\", create_by \"createBy\" " +
	 "from sys_role where id IN (SELECT role_id FROM sys_role_org WHERE org_id=#{orgId})")
	@RequestMapping("SysRoleModel")
	public List<SysRoleModel> queryRoleByOrgId(@Param("orgId")String orgId);
	
	/**
	 * 获取角色下的所有用户编号
	 * @param roleId
	 * @return
	 */
	@Select("SELECT userid FROM sys_role_user WHERE roleId=#{roleId}")
	public List<String> queryUserIdByRoleId(@Param("roleId")String roleId);
	
	/** 获取角色下所关联的菜单编号
	 * @param roleId
	 * @return
	 */
	@Select("SELECT functionid FROM sys_role_function WHERE roleId=#{roleId}")
	public List<String> queryFunctionIdByRoleId(@Param("roleId")String roleId);
	
	/** 获取菜单关联的角色菜单关联关系
	 * @param funcId
	 * @return
	 */
	@Select("SELECT id, operation, functionid, roleid, datarule FROM sys_role_function WHERE functionid=#{funcId}")
	@RequestMapping("SysRoleFunctionModel")
	public List<SysRoleFunctionModel> queryRoleFunctionByFuncId(@Param("funcId")String funcId);
	
	/**
	 * 删除角色关联的用户
	 * @param roleId
	 * @return
	 */
	@Delete("DELETE FROM sys_role_user WHERE roleId=#{roleId}")
	public Integer deleteRoleUserByRoleId(@Param("roleId")String roleId);
	
	/**
	 * 添加角色机构关联关系
	 * @param models
	 * @return
	 */
	public Integer addRoleOrg(List<SysRoleOrgModel> models);
	
	/**
	 * 删除机构下的角色关联的机构关系
	 * @param roleId
	 * @return
	 */
	@Delete("DELETE FROM sys_role_org WHERE org_id=#{orgId}")
	public Integer deleteRoleOrgByOrgId(@Param("orgId")String orgId);
	
}
